﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SF.Common;
using System.Data;

namespace SF.Ucenter.admin.contract
{
    public partial class contract_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string _action = DTRequest.GetQueryString("action");
                if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
                {
                    this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                    this.id = DTRequest.GetQueryInt("id");
                    if (this.id == 0)
                    {
                        JscriptMsg("传输参数不正确！", "back");
                        return;
                    }
                    if (!new BLL.sf_contract().Exists(this.id))
                    {
                        JscriptMsg("记录不存在或已被删除！", "back");
                        return;
                    }
                }

                if (!Page.IsPostBack)
                {
                    ChkAdminLevel("contract_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                    NumBind(ddlnum);
                    if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                    {
                        ddlnum.Enabled = false;
                        ShowInfo(this.id);
                    }

                    if (action == DTEnums.ActionEnum.Add.ToString())
                    {
                        txtcontractnum.Text = "C" + DateTime.Now.ToString("yyyyMMddHHmmss");
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "')</script>");
            }
        }

        #region 赋值编号=================================
        private void NumBind(DropDownList ddl)
        {
            BLL.manager bll = new BLL.manager();
            string str = "type = 1";
            DataTable dt = bll.GetList(str).Tables[0];

            ddl.Items.Clear();
            ddl.Items.Add(new ListItem("请选择商户编号...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                ddl.Items.Add(new ListItem(dr["busnieseNum"].ToString(), dr["id"].ToString()));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.sf_contract bll = new BLL.sf_contract();
            Model.sf_contract model = bll.GetModel(_id);

            BLL.manager man = new BLL.manager();
            string num = man.GetNum(model.busnieseNum).ToString();

            ddlnum.SelectedValue = num.ToString();
            txtcontractnum.Text = model.contract_num;
            txtcontractname.Text = model.contract_name;
            txtagreement.Text = model.agreement;
            txtdocument.Text = model.document;
            txtfirstpart.Text = model.firstpart;
            txtfirstrepresent.Text = model.first_represent;
            txtfirstperson.Text = model.first_person;
            txtfirstaddress.Text = model.first_address;
            txtfirstfax.Text = model.first_fax;
            txtsecondpart.Text = model.secondpart;
            txtsecondrepresent.Text = model.second_represent;
            txtsecondperson.Text = model.second_person;
            txtsecondaddress.Text = model.second_address;
            txtsecondfax.Text = model.second_fax;
            txtcreatepeople.Text = model.create_people;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.sf_contract model = new Model.sf_contract();
            BLL.sf_contract bll = new BLL.sf_contract();

            model.busnieseNum = ddlnum.SelectedItem.Text;
            model.contract_num = txtcontractnum.Text;
            model.contract_name = txtcontractname.Text;
            model.agreement = txtagreement.Text;
            model.document = txtdocument.Text;
            model.firstpart = txtfirstpart.Text;
            model.first_represent = txtfirstrepresent.Text;
            model.first_person = txtfirstperson.Text;
            model.first_address = txtfirstaddress.Text;
            model.first_fax = txtfirstfax.Text;
            model.secondpart = txtsecondpart.Text;
            model.second_represent = txtsecondrepresent.Text;
            model.second_person = txtsecondperson.Text;
            model.second_address = txtsecondaddress.Text;
            model.second_fax = txtsecondfax.Text;
            model.create_people = txtcreatepeople.Text;
            model.create_date = DateTime.Now;
            int a = int.Parse(bll.GetCount(model.busnieseNum).ToString());
            if (a == 1)
            {
                Response.Write("<script>alert('该商户编号已存在,请重新选择...')</script>");
            }
            else
            {
                if (bll.Add(model) > 0)
                {
                    AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加合同:" + model.contract_num); //记录日志
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.sf_contract bll = new BLL.sf_contract();
            Model.sf_contract model = bll.GetModel(_id);

            model.busnieseNum = ddlnum.SelectedItem.Text;
            model.contract_num = txtcontractnum.Text;
            model.contract_name = txtcontractname.Text;
            model.agreement = txtagreement.Text;
            model.document = txtdocument.Text;
            model.firstpart = txtfirstpart.Text;
            model.first_represent = txtfirstrepresent.Text;
            model.first_person = txtfirstperson.Text;
            model.first_address = txtfirstaddress.Text;
            model.first_fax = txtfirstfax.Text;
            model.secondpart = txtsecondpart.Text;
            model.second_represent = txtsecondrepresent.Text;
            model.second_person = txtsecondperson.Text;
            model.second_address = txtsecondaddress.Text;
            model.second_fax = txtsecondfax.Text;
            model.create_people = txtcreatepeople.Text;
            model.update_date = DateTime.Now;


            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改商户合同:" + model.contract_num); //记录日志
                result = true;
            }

            return result;
        }
        #endregion

        //提交操作
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("contract_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    if (this.id == 1)
                    {
                        JscriptMsg("不可禁用超级用户！", "contract_list.aspx");
                    }
                    else
                    {
                        JscriptMsg("保存过程中发生错误！", "");
                    }
                    return;
                }
                JscriptMsg("修改管理员信息成功！", "contract_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("contract_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加管理员信息成功！", "contract_list.aspx");
            }
        }
    }
}